/**
 * 
 */
package feedme.dao.impl;

import java.net.UnknownHostException;
import java.util.Date;

import java.util.List;

import org.bson.types.ObjectId;

import utils.SuperDate;
import utils.log.LogUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import feedme.bean.FoodServerBean;
import feedme.bean.OrderBean;
import feedme.dao.OrderDao;

/**
 * @author kinzhang
 *
 */
public class MongoDBOrderDaoImpl extends MongoDBBaseDaoImpl
    implements OrderDao 
{

    public List<?> findAll() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }
    
   

    @Override
    public boolean save(Object obj) throws Exception {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean delete(Object obj) throws Exception {
        // TODO Auto-generated method stub
        return false;
    }
    
    OrderBean DBObject2Bean(DBObject dbObject){
        
        if (dbObject == null) return null;
        
        OrderBean orderBean = new OrderBean();
        return orderBean;
        
    }
    
    /**
     * TODO
     */
    @Override
    public OrderBean findTodayOrder() {
        
        SuperDate now = new SuperDate();
 
        BasicDBObject query = new BasicDBObject();
        
        query.put("createtime", 
            new BasicDBObject("$gte", now.getTodayStartTime()).append("$lt", now.getTodayNextDayTime()));
            
        LogUtil.instance().info(query);
        
        DBObject dbObject;
        try {
            dbObject = getDBCollection().findOne(query);
           
        } catch (UnknownHostException e) {
           
            e.printStackTrace();
            return null;
        }
        
        return DBObject2Bean(dbObject);
    }
    /**
    @Override
    public DBObject findTodayOrder() {
        
        SuperDate now = new SuperDate();
 
        BasicDBObject query = new BasicDBObject();
        BasicDBObject dateQuery = new BasicDBObject();
        dateQuery.put("start", new BasicDBObject().put("$gte", now.getTodayStartTime()));
        dateQuery.put("end", new BasicDBObject().put("$lt", now.getTodayNextDayTime()));
        
        query.put("createtime", dateQuery);
        
        LogUtil.instance().info(query);
        
        DBObject dbObject;
        try {
            dbObject = getDBCollection().findOne(query);
        } catch (UnknownHostException e) {
           
            e.printStackTrace();
            return null;
        }
        
        return DBObject2Bean(dbObject);
    }
    */



    @Override
    public boolean createTodayOrder() {
        
       // DBObject order = new BasicDBObject();

        BasicDBObject createTime = new BasicDBObject();
        createTime.put("createtime", new Date());
        
      //  order.put("order", createTime);
        /*
        DBObject order = new BasicDBObject();
        order.put("order", v)
             */
       
        try {
            LogUtil.instance().debug("create:" + createTime);
            LogUtil.instance().debug(getDBCollection().save(createTime).getN());
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return true;
        }
        
        return false;
    }



    @Override
    public boolean selectFoodServer(String fsId) {
        
        SuperDate now = new SuperDate();
        
        BasicDBObject query = new BasicDBObject();
        
        query.put("createtime", 
            new BasicDBObject("$gte", now.getTodayStartTime()).append("$lt", now.getTodayNextDayTime()));
        
        LogUtil.instance().info("query:" + query);
        
        DBObject dbObject;
        try {
            dbObject = getDBCollection().findOne(query);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
            return true;
        }
            
      
        dbObject.put("fsId", new ObjectId(fsId));
        
        try {
            LogUtil.instance().debug(query);
            LogUtil.instance().debug(dbObject);
            getDBCollection().update(query, dbObject);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return true;
        }
        
        
        return false;
    }
}
